<?php

namespace app\index\controller;

use app\model\base\WeeklyCount;
use app\model\Category;
use app\model\Nav;
use app\model\Post;
use app\model\PostCategory;
use app\model\PostComment;
use app\model\PostCommentCountWeekly;
use app\model\User;
use app\model\UserCheckin;
use app\model\UserPostCommentCountWeekly;
use think\db\Raw;
use think\facade\View;
use think\Request;

class Index extends Common
{
  /**
   * 显示资源列表
   *
   * @return \think\Response
   */
  public function index()
  {

    $list_top_post = Post::where('type', 2)
      ->where('is_top', 1)
      ->where('status', 1)
      ->where('is_verify', 1)
      ->order('sort desc')
      ->limit(5)
      ->select();

    $list_index_post = Post::searchPage($this->request);

    $list_index_notice = Nav::getListIndexNav('notice');


    $is_checkin = UserCheckin::isCheckin();

    $list_comments_users = UserPostCommentCountWeekly::order('count desc')->where('week_date', WeeklyCount::parseDate())->limit(12)->select();

    $list_comments_posts = PostCommentCountWeekly::order('count desc')->where('week_date', WeeklyCount::parseDate())->limit(10)->select();

    $checkin_last_times = 0;

    if (!empty($is_checkin)) {
      $checkin_last_times = $is_checkin->last_times;
    } else {

      $last_checkin = UserCheckin::isCheckin(null, date('d') - 1);

      if (!empty($last_checkin)) {
        $checkin_last_times = $last_checkin->last_times;
      }
    }

    View::assign('list_comments_users', $list_comments_users);
    View::assign('list_comments_posts', $list_comments_posts);
    View::assign('is_checkin', $is_checkin);
    View::assign('checkin_last_times', $checkin_last_times);
    View::assign('list_index_notice', $list_index_notice);

    View::assign('list_top_post', $list_top_post);
    View::assign('list_index_post', $list_index_post);

    return View::fetch();
  }


  /**
   * 显示创建资源表单页.
   *
   * @return \think\Response
   */
  public function create()
  {
    //
  }

  /**
   * 保存新建的资源
   *
   * @param  \think\Request  $request
   * @return \think\Response
   */
  public function save(Request $request)
  {
    //
  }

  /**
   * 显示指定的资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function read($id)
  {
    //
  }

  /**
   * 显示编辑资源表单页.
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function edit($id)
  {
    //
  }

  /**
   * 保存更新的资源
   *
   * @param  \think\Request  $request
   * @param  int  $id
   * @return \think\Response
   */
  public function update(Request $request, $id)
  {
    //
  }

  /**
   * 删除指定资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function delete($id)
  {
    //
  }
}
